package cn.kgc.crowdfundingadmin.service;

import cn.kgc.crowdfundingadmin.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin.pojo.Admin;
import cn.kgc.crowdfundingadmin.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author shkstart
 * @create 2020-02-23 9:28
 */
@Service("adminService")
public class AdminServiceImpl implements  AdminService {
    @Resource
    AdminMapper adminMapper;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public Admin selectByPrimaryKey(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Admin login(String loginAcct, String userPswd) {
        //查询 loginAcct
        AdminExample adminExample=new AdminExample();
        adminExample.createCriteria().andLoginacctEqualTo(loginAcct);
        List<Admin> list = adminMapper.selectByExample(adminExample);
        //判断 如果账号为空的话 则返回null
        if(CrowdFundingUtils.collectionEffective(list)==false){
            return null;
        }
        //给admin 赋值为空
        Admin admin=list.get(0);
            if (admin==null){
                return null;
            }
            //给密码进行MD5加密
            String md5=CrowdFundingUtils.md5(userPswd);
            //判断密码 被MD5方式加密成功
            if(Objects.equals(md5,admin.getUserpswd())){
                return admin;  //成功则登陆成  否则失败
            }
            return null;
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        /*设置当前值*/
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        /*keyword没有值的话 查询全部 ,有值根据账号或者邮箱 邮箱地址查询*/
        if(CrowdFundingUtils.stringEffective(keyword)){
            AdminExample example=new AdminExample();
            AdminExample.Criteria criteria1 = example.createCriteria();
            //根据账号查询 模糊
            criteria1.andLoginacctLike("%"+keyword+"%");
            AdminExample.Criteria criteria2 = example.createCriteria();
            /*根据邮箱模糊查询*/
            criteria2.andEmailLike("%"+keyword+"%");
            AdminExample.Criteria criteria3 = example.createCriteria();
            /*根据地址进行模糊查询*/
            criteria3.andUsernameLike("%"+keyword+"%");

            example.or(criteria2);
            example.or(criteria3);
            List<Admin> list = adminMapper.selectByExample(example);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return  pageInfo;
        }else{
            /*查询全部   如果值为空的 则查询全部 判断*/
            List<Admin> list = adminMapper.selectByExample(null);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return  pageInfo;
        }
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        //
        /*AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);
        adminMapper.deleteByExample(example);*/
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);

        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        String userPswd=admin.getUserpswd();
        //获得密码 加密成md5的格式
//        userPswd=CrowdFundingUtils.md5(userPswd);
        userPswd=bCryptPasswordEncoder.encode(userPswd);
        admin.setUserpswd(userPswd);
        //执行保存  进行增加
        adminMapper.insert(admin);
    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        //对密码进行加密
        if(CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String userPswd=admin.getUserpswd();
            //获得密码 加密成md5的格式
//            userPswd=CrowdFundingUtils.md5(userPswd);
            userPswd=bCryptPasswordEncoder.encode(userPswd);
            admin.setUserpswd(userPswd);
        }
            adminMapper.updateByPrimaryKeySelective(admin);
    }
}
